package com.molyfun.parents.activity.course;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.common.attachment.AttachmentViewer;
import com.molyfun.parents.activity.course.bean.CourseMediaBean;
import com.molyfun.parents.activity.course.view.courseitem.CourseCellFactory;
import com.molyfun.parents.rest.bean.CourseDetailResponse;
import com.molyfun.parents.rest.bean.CourseListResponse;
import com.molyfun.parents.utils.Const;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:11/4/16.
 */
public class CourseDetailActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.base_title)
    TextView mTitle;
    @InjectView(R.id.base_title_left)
    ImageView mTitleBack;
    @InjectView(R.id.course_content)
    LinearLayout mCourseContentLayout;
    @InjectView(R.id.courseware_list)
    AttachmentViewer mCoursewareList;
    @InjectView(R.id.record_list)
    AttachmentViewer mRecordViewList;
    @InjectView(R.id.feedback_layout)
    TextView mFeedbackLayout;

    @InjectView(R.id.courseware_null)
    TextView mCourseWareNull;
    @InjectView(R.id.record_null)
    TextView mRecordNull;

    private static final String FLAG_COURSE_FROM_TEAM = "from_team_course";

    private CourseListResponse.CourseBean mCourseBean;
    private String mCourseTitleText = "";
    private String mCourseTargetText = "";
    private String mCourseContentText = "";

    private List<CourseMediaBean> mDataList;
    private List<CourseMediaBean> mRecordList;

    public static void startActivity(Context context, CourseListResponse.CourseBean courseBean) {
        Intent intent = new Intent();
        intent.putExtra(Const.COURSE_BEAN, courseBean);
        intent.setClass(context, CourseDetailActivity.class);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, CourseListResponse.CourseBean courseBean, boolean isFromTeam) {
        Intent intent = new Intent();
        intent.putExtra(Const.COURSE_BEAN, courseBean);
        intent.putExtra(FLAG_COURSE_FROM_TEAM, isFromTeam);
        intent.setClass(context, CourseDetailActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_detail);
        ButterKnife.inject(this);
        getSupportActionBar().setTitle("");

        mTitle.setText("课程详情");
        mTitle.setMaxLines(1);
        mTitleBack.setOnClickListener(this);


        mCourseContentLayout.setVisibility(View.VISIBLE);
//        TextView mCourseTitle = (TextView) mCourseContentLayout.findViewById(R.id.course_content_dialog_title);
        TextView mCourseTarget = (TextView) mCourseContentLayout.findViewById(R.id.course_content_dialog_target);
        TextView mCourseContent = (TextView) mCourseContentLayout.findViewById(R.id.course_content_dialog_content);
        TextView mAccomplish = (TextView) mCourseContentLayout.findViewById(R.id.course_content_accomplish);
        TextView mSummary = (TextView) mCourseContentLayout.findViewById(R.id.course_content_summary);


        mTitleBack.setOnClickListener(this);
        mFeedbackLayout.setOnClickListener(this);

        mCourseBean = getIntent().getParcelableExtra(Const.COURSE_BEAN);
        mCourseTitleText = mCourseBean.getCoursename();//getIntent().getStringExtra("courseTitle");
        mCourseTargetText = mCourseBean.getCoursetarget();//getIntent().getStringExtra("courseTarget");
        mCourseContentText = mCourseBean.getCourseprocess();//getIntent().getStringExtra("courseContent");
//        System.out.println("mCourseTitleText" + mCourseTitleText);
//        System.out.println("mCourseTargetText" + mCourseTargetText);
//        System.out.println("mCourseContentText" + mCourseContentText);
        if (!TextUtils.isEmpty(mCourseTitleText)) {
//            mTitle.setVisibility(View.VISIBLE);
            mTitle.setText(mCourseTitleText);
        }
        if (!TextUtils.isEmpty(mCourseTargetText)) {
            mCourseTarget.setVisibility(View.VISIBLE);
            mCourseTarget.setText(mCourseTargetText);
        } else {
            mCourseTarget.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(mCourseContentText)) {
            mCourseTarget.setVisibility(View.VISIBLE);
            mCourseContent.setText(mCourseContentText);
        } else {
            mCourseContent.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(mCourseBean.getResultname())) {
            mAccomplish.setText(mCourseBean.getResultname());
        } else {
            mAccomplish.setText( "暂无评分");
        }

        boolean isFromTeam = getIntent().getBooleanExtra(FLAG_COURSE_FROM_TEAM, false);
        if(!isFromTeam){
            mFeedbackLayout.setVisibility(View.VISIBLE);
            if(!TextUtils.isEmpty(mCourseBean.getUsername())){
                mTitle.setText(mCourseTitleText + "(" + mCourseBean.getUsername() + ")");
            }
        }

        if (!TextUtils.isEmpty(mCourseBean.getCourseperformance())) {
            mSummary.setText(mCourseBean.getCourseperformance());
//            etv.setMarqueeRepeatLimit(3);
        } else {
            mSummary.setText("暂无课堂表现");
        }

        getCourseDetail();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.course_toolbar_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        getFilterCourse(item);
        return false;
    }

    private void getFilterCourse(MenuItem item) {
        List<CourseMediaBean> filterCourses = new ArrayList<>();
        List<CourseMediaBean> coursesSources = new ArrayList<>();

       /* if (mCourseDetailTopBar.getCurrentChooseTab() == TwoTabBar.TwoTabChooseTab.BAR_ONE) {
            coursesSources = mDataList;
        } else {
            coursesSources = mRecordList;
        }*/
        coursesSources = mDataList;
        if (coursesSources == null || coursesSources.size() == 0) {
            return;
        }
        switch (item.getItemId()) {
            case R.id.action_video:
                for (CourseMediaBean courseMediaBean : coursesSources) {
                    if (courseMediaBean.getType() == CourseCellFactory.CourseType.COURSE_VIDEO) {
                        filterCourses.add(courseMediaBean);
                    }
                }
                break;
            case R.id.action_music:
                for (CourseMediaBean courseMediaBean : coursesSources) {
                    if (courseMediaBean.getType() == CourseCellFactory.CourseType.COURSE_MUSIC) {
                        filterCourses.add(courseMediaBean);
                    }
                }
                break;
            case R.id.action_game:
                for (CourseMediaBean courseMediaBean : coursesSources) {
                    if (courseMediaBean.getType() == CourseCellFactory.CourseType.COURSE_GAME) {
                        filterCourses.add(courseMediaBean);
                    }
                }
                break;
            case R.id.action_pic:
                for (CourseMediaBean courseMediaBean : coursesSources) {
                    if (courseMediaBean.getType() == CourseCellFactory.CourseType.COURSE_IMAGE) {
                        filterCourses.add(courseMediaBean);
                    }
                }
                break;
            case R.id.action_all:
                filterCourses = coursesSources;
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.feedback_layout:
                if (mCourseBean.getFeedbacktimes() == 0) {

                    CourseFeedbackEditActivity.startActivity(CourseDetailActivity.this, mCourseBean.getId(), mCourseBean.getDkey(), null);

//                    new CourseFeedbackDialogFragment.CourseFeedbackDialogBuilder(mCourseBean.getId(), mCourseBean.getDkey())
//                            .setCourseFeedbackCallback(new CourseFeedbackDialogFragment.CourseFeedbackCallback() {
//                                @Override
//                                public void onFeedbackSuccess() {
//                                    mCourseBean.setFeedbacktimes(mCourseBean.getFeedbacktimes() + 1);
//
//                                }
//
//                                @Override
//                                public void onFeedbackUpdateSuccess(CourseFeedbackEditRequest courseFeedbackEditRequest) {
//
//                                }
//                            })
//                            .build(getSupportFragmentManager().beginTransaction());
                } else {
                    CourseFeedbackListActivity.startActivity(CourseDetailActivity.this, mCourseBean.getId(), Const.CURRENT_COURSE_DAY_STR, mCourseBean);
                }
                break;
        }
    }

    private void getCourseDetail() {
        showProgressDialog();
        Call<CourseDetailResponse> call = MFPApplication.getInstance().getRestClient().getCourseDetail(mCourseBean.getId(), Const.FALSE);
        call.enqueue(new Callback<CourseDetailResponse>() {
            @Override
            public void onResponse(Call<CourseDetailResponse> call, Response<CourseDetailResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    mDataList = response.body().getData().getMediaobjectlist();
                    mRecordList = response.body().getData().getPlanmediaobjectlist();
                    mCourseContentLayout.setVisibility(View.VISIBLE);
                    if(mDataList != null && mDataList.size() > 0){
                        mCoursewareList.initData(mDataList);
                    }else{
                        mCoursewareList.setVisibility(View.GONE);
                        mCourseWareNull.setVisibility(View.VISIBLE);
                    }

                    if(mRecordList != null && mRecordList.size() > 0){
                        mRecordViewList.initData(mRecordList);
                    }else{
                        mRecordViewList.setVisibility(View.GONE);
                        mRecordNull.setVisibility(View.VISIBLE);
                    }

                }
            }

            @Override
            public void onFailure(Call<CourseDetailResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

}
